package com.example.webflux.demo;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

@RestController
public class LongPollingController {

    final LongPollingService longPollingService;

    public LongPollingController(LongPollingService longPollingService) {
        this.longPollingService = longPollingService;
    }

    @GetMapping("/get")
    public Mono<String> get(@RequestParam(defaultValue = "14500") Integer timeout) {
        if (timeout == 0) {
            // 首次加载
            return getValue();
        }
        return longPollingService.polling(timeout);
    }

    private Mono<String> getValue() {
        return Mono.just("0");
    }

    @GetMapping("/change")
    public Mono<String> put(@RequestParam String data) {
        return longPollingService.change(data);
    }

}
